from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet  #导入试图集
from rest_framework.generics import ListAPIView
from meiduo_admin.serializers.skus import SKUSerializers,CategorieSerialzier,GoodsSimpleSerialzier,SPUSpecificationSerializer
from meiduo_admin.utils import PageNum
from goods.models import SKU,GoodsCategory,SPU,SPUSpecification




class SKUViewSet(ModelViewSet):
    serializer_class = SKUSerializers
    queryset = SKU.objects.all()
    pagination_class = PageNum

    def categorie(self,request):
        """
            获取三级分类信息
        :param request:
        :return:
        """
        #1、查询三级分类信息
        data=GoodsCategory.objects.filter(subs=None)

        #2、序列化返回三级分类
        ser=CategorieSerialzier(data,many=True)

        return Response(ser.data)

    def goodssimple(self,request):
        #查询SPU表数据
        data = SPU.objects.all()
        # 序列化返回三级分类
        ser = GoodsSimpleSerialzier(data, many=True)
        return Response(ser.data)

    def spuspecification(self, request, pk):
        # 查询spu商品的所有规格
        data = SPUSpecification.objects.filter(spu_id=pk)
        # 序列化返回
        ser = SPUSpecificationSerializer(data, many=True)
        return Response(ser.data)
